﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch10CardLib
{
    public class Deck
    {
        private Card[] cards;

        public Deck()
        {
            cards = new Card[52];
            for (int suit = 0; suit < 4; suit++)
            {
                for (int rank = 1; rank <= 13; rank++)
                {
                    cards[suit * 13 + rank - 1] = new Card((Suit)suit, (Rank)rank);
                }
            }
        }

        public Card GetCards(int cardNum)
        {
            if (cardNum >= 0 && cardNum <= 52)
                return cards[cardNum];
            else
                throw  new ArgumentException("Invalid card number!");
        }

        public void Shuffle()
        {
            Card[] newDeck = new Card[52];
            bool[] assinged = new bool[52];
            Random rand = new Random();
            for (int i = 0; i < 52; i++)
            {
                int destCard = 0;
                bool foundCard = false;
                while (!foundCard)
                {
                    destCard = rand.Next(52);
                    if (!assinged[destCard])
                    {
                        foundCard = true;
                    }
                }
                assinged[destCard] = true;
                newDeck[destCard] = cards[i];
            }
            newDeck.CopyTo(cards, 0);
        }
    }
}
